package ru.fsd.sokoban;

import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import ru.fsd.sokoban.gui.GameFrame;

public class MenuItemActionListener implements ActionListener {

	GameFrame frame;
	
	public MenuItemActionListener(GameFrame frame) {
		this.frame = frame;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		JMenuItem mi = (JMenuItem) e.getSource();
		
		if(mi.getLabel().equals("Open level"))
			openLevel();
		else if(mi.getLabel().equals("About"))
			openAboutPanel();
		else if(mi.getLabel().equals("Reload level"))
			reloadLevel();
	}
	
	private void openLevel() {
		JFileChooser fileChooser = new JFileChooser("data/levels/");
		
		if(fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
			String pathToFile = fileChooser.getSelectedFile().getPath();
			frame.reloadLevel(pathToFile);
		}
	}
	
	private void openAboutPanel() {
		JOptionPane.showMessageDialog(
				frame,
				"Authors Sabitov A.F, Bazhin A.D, Loginov A.S.",
				"About",
				JOptionPane.INFORMATION_MESSAGE);
	}
	
	private void reloadLevel() {
		frame.reloadLevel();
	}
}
